package com.tust.db;

// 导入包
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/*
 * 数据库连接
 */

public class DBUtil {

    private static final String url = "jdbc:mysql://localhost:3306/schools?user=root&password=Qb25172142&serverTimezone=UTC";

    public static Connection getConnection() {

        Connection connection = null;

        // jdbc驱动
        String driver = "com.mysql.cj.jdbc.Driver";

        try {
            // 注册JDBC驱动程序
            Class.forName(driver);
            // 建立连接
            connection = DriverManager.getConnection(url);

        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有安装");

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }

        // 如果连接成功，返回连接 否则返回空
        if (connection != null)
            return connection;
        else
            return null;

    }

}